package com.example.foodproject.controller;

import com.example.FoodProject.bean.Food;
import com.example.FoodProject.dao.FoodDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Autowired
    FoodDao dao;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(){
        List<Food> list = dao.findAll();
        Map<String,Object> map = new HashMap<>();
        if (list!=null&&list.size()!=0){
            map.put("success",true);
            map.put("message","成功");
            map.put("list",list);
        }else{
            map.put("success",false);
            map.put("message","失败");
        }
        return map;
    }

    @RequestMapping("/findone/{id}")
    @ResponseBody
    public Map<String,Object> findone(@PathVariable(name = "id") Integer id){
        Food f = new Food(id);
        System.out.println(f);
        Food food = dao.findOne(Example.of(f)).orElse(null);
        Map<String,Object> map = new HashMap<>();
        if (food!=null){
            map.put("success",true);
            map.put("message","成功");
            map.put("list",food);
        }else{
            map.put("success",false);
            map.put("message","失败");
        }
        return map;
    }
}
